package com.shujia.json;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;

public class Deno1Gson {

    public static void main(String[] args) {

        /**
         *
         * json to  java bean
         *
         */

        String json = "{\"id\":\"1500100001\",\"name\":\"施笑槐\",\"age\":22,\"gender\":\"女\",\"clazz\":\"文科六班\"}";

        Gson gson = new Gson();

        Student student = gson.fromJson(json, Student.class);

        System.out.println(student);

        /**
         *  java bean to json
         *
         */

        String s = gson.toJson(student);
        System.out.println(s);


        /**
         * arrayList to json
         *
         */
        ArrayList<Student> students = new ArrayList<>();

        students.add(new Student(",1", "1", 2, "1", "1"));
        students.add(new Student(",1", "1", 2, "1", "1"));
        students.add(new Student(",1", "1", 2, "1", "1"));

        String jsons = gson.toJson(students);

        System.out.println(jsons);



        /**
         * json to arrayList
         *
         */

        ArrayList<Student> stus = gson.fromJson(jsons,new TypeToken<ArrayList<Student>>(){}.getType());

        System.out.println(stus);


    }
}
